<?php
/**
 * Created by PhpStorm.
 * User: wuxiaojie
 * Date: 2018/7/26
 * Time: 22:44
 */

define('IN_AJAX', TRUE);


if (!defined('IN_ANWSION'))
{
    die;
}
class aomas_tools_ajax extends AWS_ADMIN_CONTROLLER
{

    public function setup()
    {
        HTTP::no_cache_header();
    }

    public function img_upload_action()
    {
        if (!$this->user_info['permission']['is_administortar'])
        {
            H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('你没有访问权限, 请重新登录')));
        }

        $upload_path = '/aomas_tools/images/'.date("Ymd").'/';
        $file_name = 'aomas_'.time().'_'.rand(1,100).'.jpg';
        $orig_name = htmlspecialchars($_FILES['aws_upload_file']['name']);

        AWS_APP::upload()->initialize(array(
            'allowed_types' => 'jpg,png,jpeg',
            'upload_path' => get_setting('upload_dir') . $upload_path,
            'is_image' => true,
            'file_name' => $file_name,
            'encrypt_name' => FALSE
        ))->do_upload('aws_upload_file');

        if (AWS_APP::upload()->get_error())
        {
            switch (AWS_APP::upload()->get_error())
            {
                default:
                    H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('错误代码') . ': ' . AWS_APP::upload()->get_error()));
                    break;

                case 'upload_invalid_filetype':
                    H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('文件类型无效')));
                    break;
            }
        }

        if (! $upload_data = AWS_APP::upload()->data())
        {
            H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('上传失败, 请与管理员联系')));
        }
        $this->model('goods')->add_cover(1,'global', $orig_name,htmlspecialchars(get_setting('upload_url') . $upload_path), time(), basename($upload_data['full_path']), true);

        echo htmlspecialchars(json_encode(array(
            'success' => true,
            'thumb' => get_setting('upload_url') . $upload_path . basename($upload_data['full_path'])
        )), ENT_NOQUOTES);
    }

    public function mp4_upload_action()
    {
        if (!$this->user_info['permission']['is_administortar'])
        {
            H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('你没有访问权限, 请重新登录')));
        }

        $upload_path = '/aomas_tools/video/'.date("Ymd").'/';
        $file_name = 'aomas_'.time().'_'.rand(1,100).'.mp4';
        $orig_name = htmlspecialchars($_FILES['aws_upload_file']['name']);
        AWS_APP::upload()->initialize(array(
            'allowed_types' => 'mp4',
            'upload_path' => get_setting('upload_dir') . $upload_path,
            'is_image' => false,
            'file_name'=>$file_name,
            'encrypt_name' => FALSE

        ))->do_upload('aws_upload_file');

        if (AWS_APP::upload()->get_error())
        {
            switch (AWS_APP::upload()->get_error())
            {
                default:
                    H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('错误代码') . ': ' . AWS_APP::upload()->get_error()));
                    break;

                case 'upload_invalid_filetype':
                    H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('文件类型无效')));
                    break;
            }
        }

        if (!$upload_data = AWS_APP::upload()->data())
        {
            H::ajax_json_output(AWS_APP::RSM(null, -1, AWS_APP::lang()->_t('上传失败, 请与管理员联系')));
        }

//        if ($upload_data['is_image'] == 1)
//        {
//            AWS_APP::image()->initialize(array(
//                'quality' => 90,
//                'source_image' => $upload_data['full_path'],
//                'new_image' => $upload_data['full_path'],
//                'width' => 50,
//                'height' => 50
//            ))->resize();
//        }
        $this->model('goods')->add_cover(2,'global', $orig_name,htmlspecialchars(get_setting('upload_url') .$upload_path), time(), basename($upload_data['full_path']), false);


//        $this->model('menu')->update_nav_menu($_GET['id'], array('icon' => basename($upload_data['full_path'])));

        echo htmlspecialchars(json_encode(array(
            'success' => true,
            'thumb' => get_setting('upload_url') . $upload_path . basename($upload_data['full_path'])
        )), ENT_NOQUOTES);
    }

}